from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class CoursePage:
    def __init__(self, driver):
        self.driver = driver

    def find_course_exam(self):
        texts = ["课程考试"]
        for text in texts:
            try:
                xpath = f"//a[text()='{text}']"
                elem = WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, xpath)))
                return elem  # Success! Return the found element.
            except Exception as e:
                print(f"An error occurred: {e}")
        raise Exception(
            f"Failed to find element with text in {texts} due to StaleElementReferenceException.")

    def find_clicking_to_exam(self):
        xpath = '//*[@id="exam_link"]'
        elem = self.driver.find_element(By.XPATH, xpath)
        return elem

    def find_exam_record(self):
        texts = ["考试记录"]
        for text in texts:
            try:
                xpath = f"//a[text()='{text}']"
                elem = WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, xpath)))
                return elem  # Success! Return the found element.
            except Exception as e:
                print(f"An error occurred: {e}")

        raise Exception(
            f"Failed to find element with text in {texts} due to StaleElementReferenceException.")

    def find_credit_value_status(self):
        xpath = '//*[@id="creditValueStatus"]'
        elem = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, xpath)))
        return elem
